package com.zkingsoft.actions.admin.distribution;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.authority.MatrixAdapter;
import com.zkingsoft.constraint.BaseController;
import com.zkingsoft.model.distribution.DisWithdrawalsFlow;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.distribution.DisWithdrawalsFlowService;
import com.zkingsoft.util.WebUtil;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2017-06-26 07:15
 */
@Controller
@RequestMapping(value = "admin/disWithdrawalsFlow")
public class DisWithdrawalsFlowController extends BaseController {

	@Resource
	private DisWithdrawalsFlowService disWithdrawalsFlowService;

	@Resource
	private MatrixAdapter matrixAdapter;

	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "DisWithdrawalsFlow_BEV";

	public static final String fnCode = "DisWithdrawalsFlow";
	public static final String search = fnCode + ":search";
	public static final String edit = fnCode + ":edit";
	public static final String del = fnCode + ":del";
	public static final String add = fnCode + ":add";

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(
			DisWithdrawalsFlow disWithdrawalsFlow, PaginationVO pageVo) {
		matrixAdapter.isBtnPermitted(search);
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		List<DisWithdrawalsFlow> dataList = disWithdrawalsFlowService.findInPage(disWithdrawalsFlow, pageVo);
//		for(DisWithdrawalsFlow data : dataList) {
//			data.setDesName(StringUtils.convertUnicode(data.getDesName()));
//		}
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null, dataList, disWithdrawalsFlowService.findTotal(disWithdrawalsFlow));
		return result;
	}

	/**
	 * 新增或者修改页面
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(
			DisWithdrawalsFlow disWithdrawalsFlow) {
		if (disWithdrawalsFlow.getFlowId() != null) {
			matrixAdapter.isBtnPermitted(edit);
			disWithdrawalsFlow.setFlowId(((DisWithdrawalsFlow) WebUtil
					.getSessionAttribute(BEV)).getFlowId());
			AjaxResult result = modify(disWithdrawalsFlowService,
					WebUtil.getSessionAttribute(BEV), disWithdrawalsFlow,
					"提现记录");
			WebUtil.removeSessionAttribute(BEV);
			return result;
		} else {
			matrixAdapter.isBtnPermitted(add);
			return add(disWithdrawalsFlowService, disWithdrawalsFlow, "提现记录");
		}
	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		matrixAdapter.isBtnPermitted(edit);
		DisWithdrawalsFlow disWithdrawalsFlow;
		if (id != null) {
			disWithdrawalsFlow = disWithdrawalsFlowService.findById(id);
			WebUtil.getRequest().setAttribute("obj", disWithdrawalsFlow);
			WebUtil.setSessionAttribute(BEV, disWithdrawalsFlow);
		}
		return "admin/distribution/DisWithdrawalsFlow-form";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		matrixAdapter.isBtnPermitted(del);
		return remove(disWithdrawalsFlowService, keys);
	}

	/**
	 * 支付
	 */
	@RequestMapping(value= "/pay")
	@ResponseBody
	public AjaxResult pay(Long flowId) {
		DisWithdrawalsFlow disWithdrawalsFlow = disWithdrawalsFlowService.findById(flowId);
		if(disWithdrawalsFlow != null) {
			disWithdrawalsFlow.setFlowState("2");
			int i = disWithdrawalsFlowService.modifyByModel(disWithdrawalsFlow);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_OK, null, "更新成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_ERR, null, "更新失败");
			}
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "更新失败，请刷新重试");
		}
		
	}
	
	
	
}